#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Filename: get_common__info.py
# Author: ChenChangSong
# Date: 2025-11-02

"""
功能描述: 获取需求的公共信息(需求等级)
"""


import json
import time
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parent.parent.parent))

from comm.common_code import *


def req_common_get_info(req_id):
    """根据需求ID获取需求的公共信息
    req_id:需求ID
    return:priority(需求等级)
    """
    time.sleep(1)
    excel_data = get_excel_data()
    url = "https://www.tapd.cn/api/aggregation/workitem_aggregation/common_get_info"

    payload = json.dumps({
    "workspace_id": f"{excel_data['项目ID']}",
    "entity_id": req_id,
    "entity_type": "story",
    "api_controller_prefix": "",
    "enable_description": "true",
    "is_detail": 1,
    "blacklist_fields": [],
    "identifier": "app_for_editor,app_for_obj_more,app_for_obj_dialog_dropdown,app_for_obj_detail_panel,app_for_obj_detail_bottom_card,app_for_obj_attachment,app_for_obj_copy_link,app_for_obj_additional_panel",
    "installed_app_entity": {
        "obj_id": req_id,
        "obj_type": "story",
        "obj_name": "需求"
    },
    "has_edit_rule_fields": [],
    "is_archived": 0,
    "is_assistant_exec_log": 1,
    "app_id": "",
    "dsc_token": "qeSs1MkPb6GKkSam"
    })
    headers = {
    'authority': 'www.tapd.cn',
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9',
    'content-type': 'application/json',
    'cookie': excel_data["Cookie"],
    'origin': 'https://www.tapd.cn',
    'referer': f'https://www.tapd.cn/tapd_fe/{excel_data["项目ID"]}/story/detail/{req_id}',
    'sec-ch-ua': '"Not)A;Brand";v="24", "Chromium";v="116"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.97 Safari/537.36 Core/1.116.562.400 QQBrowser/19.6.6725.400'
    }

    response_code,response_data = api_request_with_retry(method="POST", url=url, headers=headers, data=payload, max_retries=3, timeout=60)
    if response_code == 200:
        logger.info(f"获取需求{req_id}的公共信息成功，响应数据：{response_data}")
        # 等级
        priority = response_data["data"]["get_info_ret"]["data"]["Story"]["custom_field_10"]
        if priority:
            logger.info(f"需求{req_id}的等级为：{priority}")
        else:
            logger.info(f"需求等级未填写")
        return priority
if __name__ == "__main__":
    req_common_get_info(req_id='1166606973001051553')
